import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
        path: '/index',
        name: 'index',
        component: resolve => require(['./views/index/Index'], resolve), //首页
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path:'/',
        redirect:'/index'
    },
    {
        path: '/trade',
        name: 'trade',
        component: resolve => require(['./views/deal/Trade'], resolve), //交易
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/stock',
        name: 'stock',
        component: resolve => require(['./views/stock/Stock'], resolve), //股票
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/safety',
        name: 'safety',
        component: resolve => require(['./views/my/Safety'], resolve), //账户安全
        meta: {
            cache: false,
            loginPower: true, //是否需要登录
        }
    },
    {
        path: '/announcement',
        name: 'announcement',
        component: resolve => require(['./views/my/Notice'], resolve), //平台公告
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/noticeDetail/:id',
        name: 'noticeDetail',
        component: resolve => require(['./views/my/NoticeDetail'], resolve), //平台公告详情
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/newPlayer',
        name: 'newPlayer',
        component: resolve => require(['./views/my/Help'], resolve), //帮助中心新手指引
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/invite',
        name: 'invite',
        component: resolve => require(['./views/my/Invite'], resolve), //邀请码
        meta: {
            cache: false,
            loginPower: true, //是否需要登录
        }
    },
    {
        path: '/aboutUs',
        name: 'aboutUs',
        component: resolve => require(['./views/index/aboutUs'], resolve), //关于我们
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/browser',
        name: 'browser',
        component: resolve => require(['./views/index/browser'], resolve), //升级游览器
        meta: {
            cache: false,
            loginPower: false, //是否需要登录
        }
    },
    {
        path: '/assets/:name',
        name: 'assets',
        component: resolve => require(['./views/property/Property'], resolve), //资产
        meta: {
            cache: false,
            loginPower: true, //是否需要登录
        },
        children: [{
            path: '/assets/usdt',
            component: resolve => require(['./views/property/Usdt'], resolve), //usdt直购
            meta: {
                cache: false,
                loginPower: true, //是否需要登录
            },
        },
        {
            path: '/assets/recharge',
            name:'recharge',
            component: resolve => require(['./views/property/Recharge'], resolve), //快速充值
            meta: {
                cache: false,
                loginPower: true, //是否需要登录
            },
        },
        {
            path: '/assets/rechargeRecords',
            name:'rechargeRecords',
            component: resolve => require(['./views/property/RechargeRecords'], resolve), //充值记录
            meta: {
                cache: false,
                loginPower: true, //是否需要登录
            },
        },
        {
            path: '/assets/withdraw',
            name:'withdraw',
            component: resolve => require(['./views/property/Withdraw'], resolve), //提现
            meta: {
                cache: false,
                loginPower: true, //是否需要登录
            },
        },
        {
            path: '/assets/withdrawRecords',
            name:'withdrawRecords',
            component: resolve => require(['./views/property/WithdrawRecords'], resolve), //提现记录
            meta: {
                cache: false,
                loginPower: true, //是否需要登录
            },
        }
        ]
    },
    {
        path: '/order/',
        name: 'order',
        component: resolve => require(['./views/order/order'], resolve),
        children: [
            {
                path: '',
                name: 'position',
                component: resolve => require(['./views/order/Position'], resolve), //持仓记录
                meta: {
                    cache: false,
                    loginPower: true, //是否需要登录
                },
                alias: "position"
            },
            {
                path: 'pending',
                name: 'pending',
                component: resolve => require(['./views/order/Pending'], resolve), //挂单记录
                meta: {
                    cache: false,
                    loginPower: true, //是否需要登录
                }
            },
            {
                path: 'history',
                name: 'history',
                component: resolve => require(['./views/order/History'], resolve), //历史记录
                meta: {
                    cache: false,
                    loginPower: true, //是否需要登录
                }
            }
        ]
    },


    ]
})
router.beforeEach((to, from, next) => {
    if (to.matched.some(res => res.meta.loginPower)) { // 判断是否需要登录权限
        if (localStorage.getItem("token")) { // 判断是否登录
            next()
        } else { // 没登录则跳转到登录界面

            next({
                path: '/',
                replace: true,
            })
        }
    } else {
        next();
    }
})

//切换路由回到顶部
router.afterEach((to, from, next) => {
    window.scrollTo(0, 0);
});
export default router